﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Timers;

namespace JTActiveSafety.Gateway.Internal
{

    public class TcpConnect : TcpChannel, IDisposable
    {

        /// <summary>
        /// 重连标志，标记连接断开后是否还需要重连，默认为false
        /// </summary>
        private volatile bool _isReconnectFlag;
        /// <summary>
        /// 连接状态
        /// </summary>
        public bool Connected { get; private set; }

        private int _reconnectTime;
        /// <summary>
        /// 重连间隔时间，默认为10秒
        /// </summary>
        public int ReconnectTime
        {
            get
            {
                return _reconnectTime;
            }
            set
            {
                if (value > 0)
                {
                    timerReconnect.Interval = value;
                }
                _reconnectTime = value;
            }
        }

        private int _receiveTime = 180;//默认3分钟
        /// <summary>
        /// 接收数据超时时间，默认为秒，不超时
        /// </summary>
        public int ReceiveTime
        {
            get
            {
                return _receiveTime;
            }
            set
            {
                if (value > 0)
                {
                    timerReciveTimeOut.Interval = value;
                }
                _receiveTime = value;
            }
        }




        private Timer timerReconnect = null;
        private Timer timerReciveTimeOut = null;

        public TcpConnect(string host, int port) : base(host, port)
        {
            timerReconnect = new Timer();
            timerReconnect.Elapsed += new System.Timers.ElapsedEventHandler(OnConnect);

            timerReciveTimeOut = new Timer();
            timerReciveTimeOut.Elapsed += new System.Timers.ElapsedEventHandler(OnReciveTimeOut);

            ReconnectTime = 10000;
            _isReconnectFlag = true;
        }

        void OnConnect(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (_isReconnectFlag && !Connected) base.Connect();
        }

        void OnReciveTimeOut(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (LastActiveTime + ReceiveTime < HostRuntime.TickSeconds)
            {
                base.Disconnect();
            }
        }

        protected override void OnDisconnect()
        {
            Connected = false;
            base.OnDisconnect();

            if (timerReciveTimeOut != null)
            {
                timerReciveTimeOut.Enabled = false;
            }

            if (timerReconnect != null)
            {
                timerReconnect.Enabled = true;
            }
        }

        protected override void OnConnected(bool flag)
        {
            Connected = flag;
            base.OnConnected(flag);

            if (!Connected)
            {
                if (timerReconnect != null)
                {
                    this.timerReconnect.Enabled = true;
                }
            }
            else
            {
                if (ReceiveTime > 0)
                {
                    if (timerReciveTimeOut != null)
                    {
                        this.timerReciveTimeOut.Enabled = true;
                    }
                }
            }
        }

        /// <summary>
        /// 断线重连
        /// </summary>
        public override void Disconnect()
        {
            base.Disconnect();
        }

        /// <summary>
        /// 断线
        /// </summary>
        /// <param name="reconnectFlag">是否需要重连</param>
        public void Disconnect(bool reconnectFlag = false)
        {
            _isReconnectFlag = reconnectFlag;
            base.Disconnect();
        }

        //bool isDisposed = false;
        //public override void Dispose()
        //{
        //    if (!isDisposed)
        //    {
        //        isDisposed = true;
        //        base.Dispose();
        //        timerReciveTimeOut.Dispose();
        //        timerReciveTimeOut = null;
        //        timerReConnect.Dispose();
        //        timerReConnect = null;
        //    }
        //}
    }
}
